package com.itic.appbase.applications.sys.syssetting.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.exception.definition.ParameterErrorException;
import com.itic.appbase.framework.utils.DateHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.applications.sys.syssetting.persistence.model.SysSetting;
import com.itic.appbase.applications.sys.syssetting.persistence.model.SysSettingExample;
import com.itic.appbase.applications.sys.syssetting.persistence.model.SysSettingExample.Criteria;
import com.itic.appbase.applications.sys.syssetting.service.SysSettingService;


@Controller
@RequestMapping(value="/sys/syssetting")
public class SysSettingController
     extends BaseController {
    @Autowired
    private SysSettingService sysSettingService;
    
    /**************************************************************************页面代码区***************************************************************/

    /**
     * 系统设置主页面
     * 
     * @param request
     * @param response
     * @return String
     */
    @RequestMapping(value = "/init")
    public String init(){
    	return "sys/syssetting/sysSettingMain";
    }

    /**
     * 系统设置添加页面
     * 
     * @param request
     * @param response
     * @return String
     */
    @RequestMapping(value = "/initAdd")
    public String initAdd(){
    	return "sys/syssetting/sysSettingAdd";
    }

    /**
     * 系统设置编辑页面
     * 
     * @param request
     * @param response
     * @return String
     */
    @RequestMapping(value = "/initEdit")
    public String initEdit( String id , Model model ){
    	if( StringHelper.isNotBlank(id) ){
    		model.addAttribute( "sysSetting", iticJsonMapper.toJson( sysSettingService.get(id) ) );
    	}else{
    		throw new ParameterErrorException();
    	}
    	return "sys/syssetting/sysSettingEdit";
    }

    /**
     * 系统设置查看页面
     * 
     * @param request
     * @param response
     * @return String
     */
    @RequestMapping(value = "/initView")
    public String initView( String id , Model model ){
    	if( StringHelper.isNotBlank(id) ) {
    		model.addAttribute(sysSettingService.get(id));
    	}else {
            throw new ParameterErrorException();
        }
    	return "sys/syssetting/sysSettingView";
    }

    /**
     * 系统设置导入页面
     * 
     * @param request
     * @param response
     * @return String
     */
    @RequestMapping(value = "/initImp")
    public String initImp(){
    	return "sys/syssetting/sysSettingImp";
    }
    
    
    
    /**************************************************************************业务代码区***************************************************************/

    @RequestMapping(value="/add")
    @ResponseBody
    public Map<String,String> add(SysSetting sysSetting){
    	 Map<String, String> resultMap = new HashMap<String, String>();
         String result = ResponseCode.SERVER_ERROR.getCode();
         Date date = new Date();
         String dateString = DateHelper.formatDateTime(date);
         sysSetting.setSysCreateTime(dateString);
         sysSetting.setSysUpdateTime(dateString);
         result = sysSettingService.add(sysSetting);
         
         resultMap.put(IticConst.RESULT, result);
         return resultMap;
    }
    
    /**
     * <p>Title:queryCondition </p>
     * <p>Description:封装查询条件 </p>
     * @return
     */
    private SysSettingExample queryCondition(HttpServletRequest request,SysSetting sysSetting, SysSettingExample example) {
        // 按创建时间排序
       example.setOrderByClause("create_time desc");
        
        Criteria criteria = example.createCriteria().andIsEffectiveEqualTo(
                DBConst.TRUE);
      
       //User user = findUserSession(request);
       //criteria.andOrgCodeEqualTo(user.getOrganizationCode());
        
       // 查询条件
       if ( null != sysSetting ) {
            if ( StringHelper.isNotBlank(sysSetting.getSysKey()) ) {
                criteria.andSysKeyLike("%" + sysSetting.getSysKey() + "%");
            }
            if( StringHelper.isNotBlank(sysSetting.getSysValue()) ){
            	 criteria.andSysValueLike("%" + sysSetting.getSysValue() + "%");
            }
        }
        return example;
    }
    
    /**
     * 
     * @param sysSetting
     * @param request
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(SysSetting sysSetting, HttpServletRequest request){
        // 声明返回对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        // 参数处理
        SysSettingExample example = new SysSettingExample();
        // 封装分页参数
        example = this.handlePageQuery(request, example);
        example = this.queryCondition(request,sysSetting, example);
        
        // 获取值班人员维护信息总条数
        int total = sysSettingService.count(example);
        // 获取数据
        List<SysSetting> list = sysSettingService.list(example);
    
        // 数据返回处理
        resultMap.put(IticConst.TOTAL_RECORDS, total);
        resultMap.put(IticConst.DISPLAY_RECORDS, total);
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        resultMap.put(IticConst.DATA, list);
        return resultMap;

    }
    
    /**
     * 
     * @param sysSetting
     * @param request
     * @return
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public Map<String,String> edit(SysSetting sysSetting, HttpServletRequest request){
    	Map<String, String> resultMap = new HashMap<String, String>();
        String result = ResponseCode.SERVER_ERROR.getCode();
        Date date = new Date();
        String dateString = DateHelper.formatDateTime(date);
        //sysSetting.setSysCreateTime(dateString);
        sysSetting.setSysUpdateTime(dateString);
        result = sysSettingService.edit(sysSetting);
        
        resultMap.put(IticConst.RESULT, result);
        return resultMap;
    }
    
    
    /**
     * 删除定期黄金类型
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, String> delete(String ids) {
        Map<String, String> resultMap = new HashMap<String, String>();
        String result = ResponseCode.SERVER_ERROR.getCode();

        //判断
        if (StringHelper.isNotBlank(ids)) {
        	
            List<String> idList = Arrays.asList(ids.split(","));
            String id = idList.get(0);
            SysSetting sysSetting = new SysSetting();
            sysSetting.setId(id);
            result = sysSettingService.delete(sysSetting);
        } else {
            result = ResponseCode.PARAM_INVALID.getCode();
        }

        resultMap.put(IticConst.RESULT, result);
        return resultMap;
    }
    
}